package org.firefly.tool.data;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class ListCompare {
    public static void main(String[] args) {
        List<Teacher> teachers = new ArrayList<>();
        teachers.add(new Teacher("张三", 24));
        teachers.add(new Teacher("李四", 23));
        teachers.add(new Teacher("王五", 25));
        System.out.println("before sort:");
        for (Teacher bean : teachers) {
            System.out.println("before sort---->age: " + bean.getAge());
        }
        //Lambda
        teachers.sort((s1, s2) -> s1.getAge().compareTo(s2.getAge()));
        System.out.println("after sort:");
        for (Teacher bean : teachers) {
            System.out.println("after sort---->age: " + bean.getAge());
        }
        //Comparator
        Collections.sort(teachers, Comparator.comparing(Teacher::getAge));
        for (Teacher bean : teachers) {
            System.out.println("after sort---->age: " + bean.getAge());
        }
        //Comparator reversed
        Comparator<Teacher> comparator = (t1, t2) -> t1.getAge().compareTo(t2.getAge());
        teachers.sort(comparator.reversed());
        for (Teacher bean : teachers) {
            System.out.println("after sort---->age: " + bean.getAge());
        }

        //组合排序
        teachers.sort(Comparator.comparing(Teacher::getAge).thenComparing(Teacher::getName));
        for (Teacher bean : teachers) {
            System.out.println("after sort---->age: " + bean.getAge() + "==name：" + bean.getName());
        }

    }
}
